package com.zzyl.nursing.controller;

import cn.hutool.json.JSONObject;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 设备Controller
 * 
 * @author cisimu
 * @date 2025-10-23
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 导出设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:export')")
    @Log(title = "设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出设备列表")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取设备详细信息")
    public R<Device> getInfo(@ApiParam(value = "设备ID", required = true)
            @PathVariable("id") Long id)
    {
        return R.ok(deviceService.selectDeviceById(id));
    }

    /**
     * 新增设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增设备")
    public AjaxResult add(@ApiParam(value = "设备实体", required = true) @RequestBody Device device)
    {
        return toAjax(deviceService.insertDevice(device));
    }



    /**
     * 删除设备
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除设备")
    public AjaxResult remove(@ApiParam(value = "要删除的数据id的数组") @PathVariable Long[] ids)
    {
        return toAjax(deviceService.deleteDeviceByIds(ids));
    }


    /**
     * 从物联网平台同步产品列表
     */
    @PostMapping("/syncProductList")
    @ApiOperation("从物联网平台同步产品列表")
    public AjaxResult sysProductList(){
        deviceService.sysProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     */
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品")
    public AjaxResult allProduct(){
        List<ProductVo> productVoList = deviceService.allProduct();
        return success(productVoList);
    }

    /**
     * 注册设备
     */
    @ApiOperation("注册设备")
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto dto){
        deviceService.register(dto);
        return success();
    }

    /**
     * 查询设备详细数据
     */
    @PostMapping("/queryDeviceDetail")
    @ApiOperation("查询设备详细数据")
    public AjaxResult queryDeviceDetail(@RequestBody DeviceDto dto){
        DeviceDetailVo vo = deviceService.queryDeviceDetail(dto.getIotId(),dto.getProductKey());
        return success(vo);

    }


    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     */
    @PostMapping("/queryProductModelInfo")
    @ApiOperation("查看指定产品的已发布物模型中的功能定义详情")
    public AjaxResult queryProductModelInfo(@RequestBody DeviceDto dto){
        JSONObject jsonObject = deviceService.queryProductModelInfo(dto.getProductKey());
        return success(jsonObject);
    }


    /**
     * 查询指定设备的物模型运行状态
     */
    @PostMapping("queryDevicePropertyStatus")
    @ApiOperation("查询指定设备的物模型运行状态")
    public AjaxResult queryDevicePropertyStatus(@RequestBody DeviceDto dto){
        Map<String,Object> map = deviceService.queryDevicePropertyStatus(dto.getDeviceName(),dto.getProductKey());
        return success(map);
    }


    /**
     * 修改设备备注名称
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改设备备注名称")
    public AjaxResult edit(@ApiParam(value = "设备实体", required = true) @RequestBody DeviceDto dto)
    {
        deviceService.updateDeviceNickName(dto);
        return success();
    }

    /**
     * 删除设备
     */
    @DeleteMapping
    @ApiOperation("删除设备")
    public AjaxResult delete(@RequestBody DeviceDto dto){
        deviceService.delete(dto);
        return success();
    }

}
